<?php


namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_team\Entity\ShusyTeam;
use Drupal\shusy_vip\Entity\ShusyVipBind;

/**
 * @OA\Schema(
 *   schema="User",
 *   type="object",
 *   description="User"
 * )
 * @OA\Property(
 *      property="uid",
 *      type="integer",
 *      description="UID系统内部唯一标识",
 * ),
 * @OA\Property(
 *      property="nickname",
 *      type="string",
 *      description="微信昵称",
 * ),
 * @OA\Property(
 *      property="avatar",
 *      type="string",
 *      description="微信头像URL",
 * ),
 */
class User extends AbstractBaseModel {

  protected function getEntityType() {
    return 'user';
  }

  protected function getLevel($uid) {
    $es = shusy_team_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyTeam) {
        //1 市场高级顾问 2 市场总监 3 市场部长 4 分红型股东
        switch ($e->level) {
          case 1:
            return '市场高级顾问';
          case 2:
            return '市场总监';
          case 3:
            return '市场部长';
          case 4:
            return '分红型股东';
        }
      }
    }
    return '';
  }

  protected function setValue() {
    if (!empty($this->entity)) {
      $avatar_file = $this->wrapper->youji_userwxinfo->avatar_file->value();
      if (!empty($avatar_file)) {
        $avatar = $this->imageStyleUrl('avatar', $avatar_file);
      }
      else {
        $default_avatar = variable_get_value('youji_userwxinfo:default-avatar');
        $default_avatar_file = file_load($default_avatar);
        $avatar = $this->imageStyleUrl('avatar', $default_avatar_file->uri);
      }
      $nickname = $this->wrapper->youji_userwxinfo->nickname->value();
      if (empty($nickname)) {
        $nickname = '匿名用户';
      }
      [$vip, $isVip] = $this->getVip($this->entity_id);
      $level = $this->getLevel($this->entity_id);
      $value = [
        'uid' => (int) $this->entity_id,
        'auth' => (bool) $this->wrapper->youji_userwxinfo->auth->value(),
        //微信昵称
        'nickname' => (string) $nickname,
        //微信头像URL
        'avatar' => (string) $avatar,
        /**
         * @OA\Property(
         *   property="vip",
         *   type="string",
         *   description="会员等级",
         * ),
         */
        'vip' => (string) $vip,
        /**
         * @OA\Property(
         *   property="level",
         *   type="string",
         *   description="内部等级",
         * ),
         */
        'level' => (string) $level,
        /**
         * @OA\Property(
         *   property="isVip",
         *   type="integer",
         *   description="是否是VIP 0 否 1 是",
         * ),
         */
        'isVip' => (int) $isVip,
        /**
         * @OA\Property(
         *   property="isTeam",
         *   type="boolean",
         *   description="是否是团队及内部推广人员 仅是内部推广人员时显示团队收益查看入口",
         * ),
         */
        'isTeam' => (bool) $isVip,
        /**
         * @OA\Property(
         *   property="isErrandAdmin",
         *   type="boolean",
         *   description="是否是快达负责人",
         * ),
         */
        'isErrandAdmin' => (bool) $this->getIsErrandAdmin(),
        /**
         * @OA\Property(
         *   property="isErrand",
         *   type="boolean",
         *   description="是否是快达人",
         * ),
         */
        'isErrand' => (bool) $this->getIsErrand(),
      ];
      $this->merge($value);
    }
    else {
      $default_avatar = variable_get_value('youji_userwxinfo:default-avatar');
      $default_avatar_file = file_load($default_avatar);
      $avatar = $this->imageStyleUrl('avatar', $default_avatar_file->uri);
      //用户不存在
      $value = [
        'uid' => (int) 0,
        //微信昵称
        'nickname' => (string) '匿名用户',
        //微信头像URL
        'avatar' => (string) $avatar,
      ];
      $this->merge($value);
    }
  }

  protected function getIsErrand() {
    $query = db_select('shusy_errand', 'l');
    $query->fields('l', ['id']);
    $query->condition('uid', $this->getUid());
    $query->condition('status', 1);
    return $query->execute()->rowCount();
  }

  protected function getIsErrandAdmin() {
    $query = db_select('shusy_errand', 'l');
    $query->fields('l', ['id']);
    $query->condition('uid', $this->getUid());
    $query->condition('is_admin', 1);
    $query->condition('status', 1);
    return $query->execute()->rowCount();
  }

  protected function getVip($uid) {
    $es = shusy_vip_bind_load_multiple(FALSE, ['uid' => $uid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyVipBind && $e->status == 1) {
        $ew = $e->wrapper();
        $name = $ew->vip->name->value();
        if (!empty($name)) {
          return [$name, 1];
        }
      }
    }
    return ['加入会员', 0];
  }
}
